package com.ruoyi.web.controller.business;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.DelFlag;
import com.ruoyi.system.domain.HuoPin;
import com.ruoyi.system.domain.HuoPinPinLei;
import com.ruoyi.system.domain.vo.ChangeGongYingShangRequest;
import com.ruoyi.system.service.IHuoPinPinLeiService;
import com.ruoyi.system.service.IHuoPinService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 货品信息 前端控制器
 * </p>
 *
 * @author qubaofeng
 * @since 2025-02-09
 */
@RestController
@RequestMapping("/system/huoPin")
public class HuoPinController extends BaseController {

    @Resource
    private IHuoPinService service;

    @Resource
    private IHuoPinPinLeiService pinPinLeiService;

    /**
     * 获取列表
     */
    @GetMapping("/page")
    public TableDataInfo listWithPage(HuoPin user) {
        startPage();
        List<HuoPin> list = service.selectWithPage(user);
        return getDataTable(list);
    }


    /**
     * 获取列表,不分页
     */
    @GetMapping("/listByCangKu")
    public AjaxResult listByCangKu(HuoPin sp) {
        AjaxResult ajax = AjaxResult.success();
        List<HuoPin> list = service.listByCangKu(sp);
        ajax.put(AjaxResult.DATA_TAG, list);
        return ajax;
    }

    /**
     * 获取列表,不分页
     */
    @GetMapping("/listByPinLei")
    public AjaxResult listByPinLei(HuoPin sp) {
        AjaxResult ajax = AjaxResult.success();
        List<HuoPin> list = service.listByPinLei(sp);
        ajax.put(AjaxResult.DATA_TAG, list);
        return ajax;
    }



    /**
     * 根据id获取详细信息
     */
    @GetMapping(value = { "/{id}"})
    public AjaxResult getInfo(@PathVariable(value = "id") Long id) {
        AjaxResult ajax = AjaxResult.success();
        HuoPin huoPin = service.getById(id);
        HuoPinPinLei pinLei = pinPinLeiService.getById(huoPin.getPinLeiId());
        huoPin.setPinLeiName(pinLei.getName());

        ajax.put(AjaxResult.DATA_TAG, huoPin);
        return ajax;
    }

    /**
     * 新增用户
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody HuoPin user) {

        if(user.getPrice() == null ){
            user.setPrice(new BigDecimal(0));
        }
        user.setCreateBy(getUsername());
        user.setCreateTime(new Date());
        return toAjax(service.save(user));
    }


    /**
     * 修改用户
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody HuoPin user) {
        user.setUpdateBy(getUsername());
        user.setUpdateTime(new Date());
        return toAjax(service.updateById(user));
    }

    /**
     * 修改用户
     */
    @PutMapping("/changeGYS")
    public AjaxResult changeGYS(@Validated @RequestBody ChangeGongYingShangRequest user) {
        user.setUpdateBy(getUsername());
        user.setUpdateTime(new Date());
        return toAjax(service.changeGongYingShang(user));
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        HuoPin param = new HuoPin();
        param.setId(id);
        param.setDelFlag(DelFlag.DEL.getCode());
        return toAjax(service.updateById(param));
    }

}
